﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DragDemo
{
    public partial class TestDragForm : Form
    {
        private Point mouse_offset;
        private Point mouse_oldPos;
        private bool isDragCom = false;

        public TestDragForm()
        {
            InitializeComponent();
            init();
        }

        private async void init()
        {
            await webView21.EnsureCoreWebView2Async();
            string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "HTMLPage1.html");
            webView21.Source = new Uri("file:///D:/ProgramFiles/DragDemo/DragDemo/HTMLPage1.html");

            webView21.CoreWebView2.NewWindowRequested += (sender2, e2) =>
            {
                String _fileurl = e2.Uri.ToString();
                e2.Handled = true;
                RunJs($"var temp_dropObject = \"{_fileurl}\"");
            };
        }

        public void RunJs(String js)
        {
            if (webView21.CoreWebView2 != null)
                webView21.CoreWebView2.ExecuteScriptAsync(js);
        }

        private void TestDragForm_Load(object sender, EventArgs e)
        {
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            //if (e.Button == MouseButtons.Left)
            //{
            //    Point mousePos = Control.MousePosition;
            //    mousePos.Offset(mouse_offset.X, mouse_offset.Y);
            //    ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
            //}
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_offset = new Point(-e.X, -e.Y);
            mouse_oldPos = ((Control)sender).Location;
            isDragCom = false;
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            ((Control)sender).Cursor = Cursors.Arrow;
            if (e.Button == MouseButtons.Left && isDragCom == false)
            {
                int xMin = webView21.Location.X;
                int yMin = webView21.Location.Y;
                int xMax = xMin + webView21.Size.Width;
                int yMax = yMin + webView21.Size.Height;
                Point mousePos = new Point(Control.MousePosition.X, Control.MousePosition.Y);
                Point mousePos2 = PointToClient(mousePos);
                mousePos.Offset(mouse_offset.X, mouse_offset.Y);
                mousePos2.Offset(mouse_offset.X, mouse_offset.Y);
                ((Control)sender).Location = ((Control)sender).Parent.PointToClient(mousePos);
                if (mousePos2.X >= xMin && mousePos2.X <= xMax && mousePos2.Y <= yMax && mousePos2.Y >= yMin)
                {
                    //拖拽到webview上了
                    label2.Text += "拖上来啦一个：" + ((Control)sender).Text + "\n";
                    ((Control)sender).Location = mouse_oldPos;
                    isDragCom = true;

                    //通信webview

                    string jhson = JsonConvert.SerializeObject(((Control)sender).Text);
                    webView21.CoreWebView2.PostWebMessageAsString(jhson);
                }
            }
        }
    }
}